home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 26 / develop issue 26 code / truffles - display mgr. / sprocket / sources / threadcontext.cp < prev    next >
Encoding:
Text File  |  1995-08-31  |  2.1 KB  |  110 lines

  1. /*
  2.     File:        ThreadContext.cp
  3.  
  4.     Contains:    An abstract class for a thread context
  5.                 
  6.     Written by: Steve Sisak
  7.     
  8.     Copyright:    © 1995 by Steve Sisak, all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.      
  12.  */
  13.  
  14. #ifndef        _THREADCONTEXT_
  15. #include    "ThreadContext.h"
  16. #endif
  17.  
  18. #include "Exceptions.h"
  19.  
  20. TThreadContext::TThreadContext()
  21. {
  22.     fThreadID        = kNoThreadID;
  23.     fThreadResult    = nil;
  24. #ifdef    __MWERKS__
  25. //    __new_exception_state(&fExceptionState,fCatchBuffer); // Use for CW 6
  26.     __new_exception_state(&fExceptionState,fCatchBuffer,sizeof(fCatchBuffer)); // Use for CW 7
  27. #endif
  28. }
  29.  
  30.  
  31. TThreadContext::~TThreadContext()
  32. {
  33. }
  34.  
  35.  
  36. void TThreadContext::CreateThread(
  37.     ThreadStyle            threadStyle,
  38.     ThreadEntryProcPtr    threadEntry,
  39.     Size                stackSize,
  40.     ThreadOptions        options)
  41. {
  42.     FailOSErr(NewThread(threadStyle, threadEntry, this, stackSize, options, &fThreadResult, &fThreadID));
  43.  
  44.     Try
  45.     {
  46.         FailOSErr(SetThreadSwitcher(fThreadID, SwitchInProc, this, true));
  47.         FailOSErr(SetThreadSwitcher(fThreadID, SwitchOutProc, this, false));
  48.         FailOSErr(SetThreadTerminator(fThreadID, TerminationProc, this));
  49.     }
  50.     Catch(err)
  51.     {
  52.         OSErr ignore = DisposeThread(fThreadID, &fThreadResult, (options & kUsePremadeThread) != 0);
  53.  
  54.         fThreadID = kNoThreadID;
  55.         
  56.         Throw(err);
  57.     }
  58. }
  59.  
  60.  
  61. void TThreadContext::SwitchIn(void)
  62. {
  63. #ifdef    __MWERKS__
  64.     __switch_exception_state(&fExceptionState, &fSavedState);
  65. #endif
  66. }
  67.  
  68.  
  69. void TThreadContext::SwitchOut(void)
  70. {
  71. #ifdef    __MWERKS__
  72.     __switch_exception_state(&fSavedState, &fExceptionState);
  73. #endif
  74. }
  75.  
  76.  
  77. void TThreadContext::Terminate(void)
  78. {
  79.     if (fThreadID != kNoThreadID)
  80.     {
  81.         fThreadID = kNoThreadID;
  82.     }
  83. }    
  84.  
  85.  
  86. pascal void TThreadContext::SwitchInProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
  87. {
  88.     TThreadContext*    thread = (TThreadContext*) switchProcParam;
  89.     
  90.     thread->SwitchIn();
  91. }
  92.  
  93.  
  94. pascal void TThreadContext::SwitchOutProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
  95. {
  96.     TThreadContext*    thread = (TThreadContext*) switchProcParam;
  97.     
  98.     thread->SwitchOut();
  99. }
  100.  
  101.  
  102. pascal void TThreadContext::TerminationProc(ThreadID /*threadBeingSwitched*/, void *terminationProcParam)
  103. {
  104.     TThreadContext*    thread = (TThreadContext*) terminationProcParam;
  105.     
  106.     thread->Terminate();
  107. }
  108.  
  109.  
  110.